package movie.test.category;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import movie.test.as.category.service.CategoryService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/categoryConntroller")
public class CategoryController {

	private static Logger logger = LoggerFactory
			.getLogger(CategoryController.class);

	@Autowired
	private CategoryService categoryServiceImpl;

	/**
	 * 电影种类的添加
	 */
	@ResponseBody
	@RequestMapping("/addCategory")
	public Object addCategory(@RequestBody JSONObject objs,
			HttpServletRequest request) {

		try {
			logger.debug("添加电影的种类开始---");
			int count = categoryServiceImpl.addCategory(objs);
			logger.info("添加电影种类完成...");
            LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
            if(count>0){
            	resultMap.put("successCode", count);
                return resultMap;
            }else{
            	resultMap.put("errorcode", "网络连接失败");
            	return resultMap;
            }
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	/**
	 * 修改电影的种类
	 */
	@ResponseBody
	@RequestMapping("/updateCategory")
	public Object updateCategory(@RequestBody JSONObject objs,	HttpServletRequest request) {
  
		try{
			logger.debug("修改电影的种类开始－－－");
			int count = categoryServiceImpl.updateCategory(objs);
			 LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
			logger.info("修改电影种类结束");
		      if(count>0){
	            	resultMap.put("successCode", count);
	                return resultMap;
	            }else{
	            	resultMap.put("errorcode", "网络连接失败");
	            	return resultMap;
	            }
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 删除电影的种类
	 */
	@ResponseBody
	@RequestMapping("/deleteCategory")
	public Object deleteCategory(@RequestBody JSONObject objs,	HttpServletRequest request) {
		try{
			logger.debug("删除电影的种类开始－－－");
			int count = categoryServiceImpl.deleteCategory(objs);
			logger.info("删除电影种类结束");
			 LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
			  if(count>0){
	            	resultMap.put("successCode", count);
	                return resultMap;
	            }else{
	            	resultMap.put("errorcode", "网络连接失败");
	            	return resultMap;
	            }
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 查看电影的种类
	 */
	@ResponseBody
	@RequestMapping("/findAllCategory")
	public Object findAllCategory(@RequestBody JSONObject objs,HttpServletRequest request) {
		try{
			logger.debug("查找电影的种类开始－－－");
		    PageInfo categoryList = categoryServiceImpl.findAllCategory(objs);
		    logger.info("查找电影种类结束");
		    Map<String,Object> map = new LinkedHashMap<String,Object>();
		    map.put("result", categoryList);
		   return map;
		}catch (Exception e) {
			e.printStackTrace();
		}
	return "";
	}
	
	/**
	 * 
	 * 前台查看电影的种类
	 * 	 */
	@ResponseBody
	@RequestMapping("/findCategory")
	public Object findCategory(@RequestBody JSONObject objs,HttpServletRequest request) {
		try{
			logger.debug("查找电影的种类开始－－－");
		   List<Map<String,Object>> categoryList = categoryServiceImpl.findCategory(objs);
		    logger.info("查找电影种类结束");
		    Map<String,Object> map = new LinkedHashMap<String,Object>();
		    map.put("result", categoryList);
		   return map;
		}catch (Exception e) {
			e.printStackTrace();
		}
	return "";
	}

	/**
	 * 根据影片的种类查询影片的信息
	 */
	public Object getMovieInfoByCategoryId(@RequestBody JSONObject objs,HttpServletRequest request){
		try{
			logger.debug("根据影片种类查询影片信息开始----");
		    PageInfo<Map<String,Object>> mapInfo = categoryServiceImpl.getMovieInfoByCategoryId(objs);
			logger.info("根据影片种类查询影片信息结束...");
			
		} catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 查询影片种类
	 */
	@ResponseBody
	@RequestMapping("/getCategoryById")
	public Object getCategoryById(@RequestBody JSONObject objs,HttpServletRequest request){
		try{
			logger.debug("根据影片种类查询影片信息开始----");
		    Map<String,Object> mapInfo = categoryServiceImpl.getCategoryById(objs);
			logger.info("根据影片种类查询影片信息结束...");
			LinkedHashMap<String,Object> result = new LinkedHashMap<String,Object>();
			result.put("result", mapInfo);
			return result;
		} catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
}
